<?php
namespace App\Model\service\admin;
use App\Model\AdminUser;
use Illuminate\Support\Facades\Cache;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/25
 * Time: 10:36
 */
class AdminUserLogic
{
    public function login($data){

        $check['name'] = $data['user_name'];
        //密码加盐
        $check['password'] = $data['password'];
        $db = new AdminUser();
        $result = $db->getAdminUserInfo($check);
        if($result) {
            //单用户多用户登录控制
            $session_data = Cache::get(config('adminRedisPrefix').$result->id);
            if($session_data){
                $ret = array('access_token'=>$session_data,'id'=>$result->id,'password'=>$result->password,'email'=>$result->email);
                $resp = new Response('200', '',$ret);
                return $resp;
            }
            //使用user_id生成token
            $access_token = Common::getInstance()->getUUID();
            Cache::store('redis')->set(config('adminRedisPrefix').$result->id,$access_token,config('admin_token_expire'));
            $ret = array('access_token'=>$access_token,'id'=>$result->id,'password'=>$result->password,'email'=>$result->email);
            $resp = new Response('200', '',$ret);
            return $resp;
        } else{
            $resp = new Response('422', '账号密码错误');
            return $resp;
        }
    }

}